import http from '@/http/http'
import { ListParam } from '../dept/DeptModel'
import { AddUserModel, AssignRoleListParam, LoginParam,LoginResult,selectRoleParams,UserInfo,UserListParams } from './userModel'

enum Api {
    getImg = '/api/sysUser/image',
    login = '/api/user/login',
    getInfo = '/api/sysUser/getInfo',
    getLeftTree = '/api/department/list',
    getUserListApi = '/api/user/list',
    add = '/api/user',
    edit = '/api/user',
    delete = '/api/user',
    getRoleList = '/api/user/getRolistForAssign',
    getRoleId = '/api/user/getRoleIdByUserId',
    assignSave = '/api/user/assingRole',
    loginOut = '/api/sysUser/loginOut',
    restore = '/api/backup/restore'
}

// 获取验证码
export async function getImageApi() {
    return await http.getImage(Api.getImg)
}

// 登录
export async function loginApi(params:LoginParam) {
    return await http.login<LoginResult>(Api.login,params)
}

// 获取用户信息
export const getInfoApi = async () => {
    return await http.get<UserInfo>(Api.getInfo)
}

// 获取用户部门树
export const getLeftTree = async () => {
    return await http.get(Api.getLeftTree)
}

// 获取用户列表
export const getUserListApi = async (params:UserListParams) => {
    return await http.get(Api.getUserListApi,params)
}

// 新增用户
export const addUserApi = async (params:AddUserModel) => {
    return await http.post(Api.add, params)
}

// 编辑用户
export const editUserApi = async (params:AddUserModel) => {
    return await http.put(Api.edit, params)
}

// 删除用户
export const deleteUserApi = async (id) => {
    return await http.delete(Api.delete, id)
}

// 获取分配角色弹框列表
export const getRoleListApi = async (params: AssignRoleListParam) => {
    return await http.get(Api.getRoleList, params)
}

// 查询用户原来拥有的角色id
export const getRoleIdApi = async (userId: number | string) => {
    return await http.getRestApi(Api.getRoleId, {userId: userId})
}

// 分配角色保存
export const assignRoleSaveApi = async (params:selectRoleParams) => {
    return await http.post(Api.assignSave,params)
}

// 退出登录
export const loginOutApi = async (params) => {
    return await http.post(Api.loginOut, params)
}

// 还原数据
export const restoreApi = async () => {
    return await http.post(Api.restore, null)
}